
//
//  NSImage+VIPExtention.m
//  fasdf
//
//  Created by My mac on 2019/3/6.
//  Copyright © 2019年 VIPCODE. All rights reserved.
//

#import "NSImage+VIPExtention.h"

@implementation NSImage (VIPExtention)

- (NSArray *)cropNum:(NSInteger)count {
    // 用来存储每个小图片
    NSMutableArray *images = [NSMutableArray array];
    
    // 屏幕的像素与点的比例
    CGFloat screenScale = 1;
    // 获取每个小图片的宽度
    CGFloat smallImageW = self.size.width / count * screenScale;
    // 获取图片的高度
    CGFloat smallImageH = self.size.height * screenScale;
    
    for (int i = 0; i < count; i++) {
        // 每个图片的矩形大小
        CGRect smallRect = CGRectMake(i * smallImageW, 0, smallImageW, smallImageH);
        // 小图
        NSImage *smallImage = [NSImage cropImage:self cropRect:smallRect];
        // 截取图片添加到数组中
        [images addObject:smallImage];
    }
    
    return images;
}

+ (NSArray *)cropImage:(NSImage *)image num:(NSInteger)count {
    return [image cropNum:count];
}

+ (instancetype)cropImage:(NSImage *)originalImage cropRect:(CGRect)frame {
    CGImageRef ref = CGImageCreateWithImageInRect(originalImage.CGImage, frame);
    NSImage *image = [NSImage imageWithCGImage:ref];
    CGImageRelease(ref);
    return image;
}

- (CGImageRef)CGImage {
    NSData * imageData = [self TIFFRepresentation];
    CGImageRef imageRef = NULL;
    if(imageData) {
        CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData,  NULL);
        imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        
    }
    return imageRef;
}

+ (instancetype)imageWithCGImage:(CGImageRef)cgImage {
    NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
    CGContextRef imageContext = nil;
    NSImage* image = nil;
    imageRect.size.height = CGImageGetHeight(cgImage);
    imageRect.size.width = CGImageGetWidth(cgImage);
    
    image = [[NSImage alloc] initWithSize:imageRect.size];
    [image lockFocus];
    imageContext = (CGContextRef)[[NSGraphicsContext currentContext]
                                  graphicsPort];
    
    CGContextDrawImage(imageContext, *(CGRect*)&imageRect, cgImage);
    [image unlockFocus];
    
    return image;
}

+ (NSArray *)tt_imageWithGif:(NSString *)gifName {
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:gifName withExtension:@"gif"];
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
//    CGImageSourceRef gifSource = CGImageSourceCreatewith
    size_t gifCount = CGImageSourceGetCount(gifSource);
    NSMutableArray *frames = [[NSMutableArray alloc]init];
    for (size_t i = 0; i< gifCount; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        NSImage *image = [NSImage imageWithCGImage:imageRef];
        
        [frames addObject:image];
        CGImageRelease(imageRef);
    }
    return frames;
}

+ (NSArray *)tt_imageWithGifPathUrl:(NSURL *)gifNamePathUrl {
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)gifNamePathUrl, NULL);
    //    CGImageSourceRef gifSource = CGImageSourceCreatewith
    size_t gifCount = CGImageSourceGetCount(gifSource);
    NSMutableArray *frames = [[NSMutableArray alloc]init];
    for (size_t i = 0; i< gifCount; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        NSImage *image = [NSImage imageWithCGImage:imageRef];
        [frames addObject:image];
        CGImageRelease(imageRef);
    }
    return frames;
}

+ (NSDictionary *)tt_imageTimeArrWithGifPathUrl:(NSURL *)gifNamePathUrl {
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)gifNamePathUrl, NULL);
    size_t gifCount = CGImageSourceGetCount(gifSource);
    float totalTime = 0;
    NSMutableArray *times = [[NSMutableArray alloc]init];
    for (size_t i = 0; i< gifCount; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        float frameDuration = 0.1f;
        CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(gifSource, i, nil);
        NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
        NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyPNGDictionary];
        
        NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
        if (delayTimeUnclampedProp)
        {
            frameDuration = [delayTimeUnclampedProp floatValue];
        }
        else
        {
            NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
            if (delayTimeProp)
            {
                frameDuration = [delayTimeProp floatValue];
            }
        }
        
        if (frameDuration < 0.011f)
        {
            frameDuration = 0.100f;
        }
        totalTime += frameDuration;
        [times addObject:@(frameDuration)];

        CFRelease(cfFrameProperties);
        CGImageRelease(imageRef);
    }
    
    if (totalTime == 0) {
        return nil;
    }
    
    NSMutableArray *keyTimeArr = [[NSMutableArray alloc] init];
    [keyTimeArr addObject:@(0)];
    for (size_t i = 0; i< times.count; i++) {
        float specificValue = [times[i] floatValue]/totalTime + [keyTimeArr[i] floatValue];
        [keyTimeArr addObject:@(specificValue)];
    }
    return @{@"keyTimeArr": keyTimeArr,@"totalTime": @(totalTime)};
}

@end
